﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var sql = "select * from Students";

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text.Trim();
            var sql = string.Format("select * from Students where Name like '%{0}%'", name);

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            var s = form2.ShowDialog();
            if (s == DialogResult.Yes)
            {
                var strSql = "select * from students";
                var dt = DbHelper.GetDataTable(strSql);
                dataGridView1.DataSource = dt;

            }
            else
            {
                MessageBox.Show("");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var name = (string)row.Cells["Name"].Value;
                var age = (int)row.Cells[2].Value;
                var score = (int)row.Cells[3].Value;
               Form2 form2 = new Form2(id, name, age, score);
                var res = form2.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var tempName = textBox1.Text.Trim();
                    var sqlStr = string.Format("select * from Students where Name like '%{0}%'", tempName);

                    var dt = DbHelper.GetDataTable(sqlStr);

                    dataGridView1.DataSource = dt;
                }
            }
            else
            {
                MessageBox.Show("您未选择任何数据行!", "提示");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var rows = dataGridView1.SelectedRows;

            if (rows.Count > 0)
            {
                var id = (int)rows[0].Cells["Id"].Value;

                var sql = string.Format("delete from Students where id={0}", id);
                var resCount = DbHelper.AddOrUpdateOrDelete(sql);

                if (resCount > 0)
                {
                    MessageBox.Show("删除成功", "提示");

                    var name = textBox1.Text.Trim();
                    var sqlStr = string.Format("select * from Students where Name like '%{0}%'", name);

                    var dt = DbHelper.GetDataTable(sqlStr);

                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("删除失败", "提示");
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的行", "提示");
            }
        }
    }
}
    

